package com.research.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.research.common.CustomException;
import com.research.entity.Teacher;
import com.research.mapper.TeacherMapper;
import com.research.service.TeacherService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;


/*登入服务实现类*/
@Slf4j
@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher> implements TeacherService {

    @Autowired
    TeacherMapper teacherMapper;

    @Override
    public Teacher loginadmin(Teacher teacher) {
        //通过Mybatis下的QueryWrapper方法实现用户名密码检验登录
        QueryWrapper<Teacher> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("teacher_password", teacher.getTeacherPassword());
        queryWrapper.eq("teacher_name", teacher.getTeacherName());
        //判断密码是否正确
        if(baseMapper.selectOne(queryWrapper)!=null){
            //判断是否具有管理员权限
            queryWrapper .and((wrapper) -> {
                wrapper.eq("teacher_title", "管理员").or().eq("teacher_title", "超级管理员");
            });
            if(baseMapper.selectOne(queryWrapper)!=null){
                return baseMapper.selectOne(queryWrapper);
            }
            else{
                throw new CustomException("权限不足");
            }
        }
        else{
            throw new CustomException("用户名或密码错误！");
        }
    }

    @Override
    public Teacher loginuser(Teacher teacher) {
        //通过Mybatis下的QueryWrapper方法实现用户名密码检验登录
        QueryWrapper<Teacher> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("teacher_password", teacher.getTeacherPassword());
        queryWrapper.eq("teacher_name", teacher.getTeacherName());
        //判断密码是否正确
        if(baseMapper.selectOne(queryWrapper)!=null){
            return baseMapper.selectOne(queryWrapper);
        }
        else{
            throw new CustomException("用户名或密码错误！");
        }
    }


    @Override
    public Teacher reg(Teacher teacher) {
        String username = teacher.getTeacherName();
        String password = teacher.getTeacherPassword();
        if (Objects.equals(username, "") || Objects.equals(password, "")) {
            throw new CustomException("输入的用户名或密码不能为空");
        }
        QueryWrapper<Teacher> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("teacher_name", username);
        Teacher tu = teacherMapper.selectOne(queryWrapper);
        if (tu != null) {
            throw new CustomException("用户名已经被注册");
        }
        teacher.setTeacherPassword(password);
        teacher.setTeacherName(username);
        Integer rows = teacherMapper.insert(teacher);
        if (rows != 1) {
            throw new CustomException("添加数据失败");
        }
        return teacher;
    }



}
